import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.ClassNotFoundException;

public class Main {
    String[] colors={"红色","绿色","蓝色"};
    //声明菜单
    JFrame f=new JFrame("测试swing窗口");
    JMenuBar jMenuBar=new JMenuBar();
    JMenu fileMenu=new JMenu("文件");
    JMenu EditMenu=new JMenu("编辑");
    JMenuItem auto=new JMenuItem("自动换行");
    JMenuItem copy=new JMenuItem("复制");
    JMenuItem paste=new JMenuItem("粘贴");
    JMenu formatMenu=new JMenu("格式");
    JMenuItem comment=new JMenuItem("注释");
    JMenuItem cancel=new JMenuItem("取消注释");
    //声明文本域
    JTextArea jTextArea=new JTextArea(8,20);
    //声明列表
    JList<String> jList=new JList<String>(colors);
    //声明选择相关组件
    JComboBox<String> colorselect=new JComboBox<String>();
    ButtonGroup bg=new ButtonGroup();
    JRadioButton male=new JRadioButton("男",false);
    JRadioButton female=new JRadioButton("女",true);
    JCheckBox isMarried=new JCheckBox("是否已婚？",true);
    JTextField tf=new JTextField(40);
    JButton ok=new JButton("确定");
    //声明右键菜单
    JPopupMenu jPopupMenu=new JPopupMenu();
    ButtonGroup popupButtonpg=new ButtonGroup();
    JRadioButtonMenuItem metal=new JRadioButtonMenuItem("Metal风格");
    JRadioButtonMenuItem nimbus=new JRadioButtonMenuItem("Nimbus风格");
    JRadioButtonMenuItem windows=new JRadioButtonMenuItem("Windows风格",true);
    JRadioButtonMenuItem windowsclassic=new JRadioButtonMenuItem("Windows经典风格");
    JRadioButtonMenuItem motif=new JRadioButtonMenuItem("Motif风格");

    public static void main(String[] args) {
        new Main().init();
    }
    public void init(){
        //组装视图
        JPanel bottomPanel=new JPanel();
        bottomPanel.add(tf);
        bottomPanel.add(ok);
        f.add(bottomPanel, BorderLayout.SOUTH);
        JPanel selectPanel=new JPanel();
        colorselect.addItem("红色");
        colorselect.addItem("绿色");
        colorselect.addItem("蓝色");
        selectPanel.add(colorselect);
        bg.add(male);
        bg.add(female);
        selectPanel.add(male);
        selectPanel.add(female);
        selectPanel.add(isMarried);
        Box topLeft=Box.createVerticalBox();
        topLeft.add(jTextArea);
        topLeft.add(selectPanel);
        Box top=Box.createHorizontalBox();
        top.add(topLeft);
        top.add(jList);
        f.add(top);
        formatMenu.add(comment);
        formatMenu.add(cancel);
        EditMenu.add(auto);
        EditMenu.addSeparator();
        EditMenu.add(copy);
        EditMenu.add(paste);
        EditMenu.addSeparator();
        EditMenu.add(formatMenu);
        jMenuBar.add(fileMenu);
        jMenuBar.add(EditMenu);
        f.setJMenuBar(jMenuBar);
        popupButtonpg.add(metal);
        popupButtonpg.add(nimbus);
        popupButtonpg.add(windows);
        popupButtonpg.add(windowsclassic);
        popupButtonpg.add(motif);
        ActionListener actionListener=new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String actionCommand=e.getActionCommand();
                try {
                    changeFlavor(actionCommand);
                } catch (UnsupportedLookAndFeelException ex) {
                    throw new RuntimeException(ex);
                } catch (ClassNotFoundException ex) {
                    throw new RuntimeException(ex);
                } catch (InstantiationException ex) {
                    throw new RuntimeException(ex);
                } catch (IllegalAccessException ex) {
                    throw new RuntimeException(ex);
                }
            }
        };
        metal.addActionListener(actionListener);
        nimbus.addActionListener(actionListener);
        windows.addActionListener(actionListener);
        windowsclassic.addActionListener(actionListener);
        motif.addActionListener(actionListener);
        jPopupMenu.add(metal);
        jPopupMenu.add(nimbus);
        jPopupMenu.add(windows);
        jPopupMenu.add(windowsclassic);
        jPopupMenu.add(motif);
        jTextArea.setComponentPopupMenu(jPopupMenu);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);
    }
    private void changeFlavor(String s) throws UnsupportedLookAndFeelException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        switch (s){
            case "Metal风格":
                String a="javax.swing.plaf.metal.MetalLookAndFeel";
                UIManager.setLookAndFeel(a);
                break;
            case "Nimbus风格":
                UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
                break;
            case "Windows风格":
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                break;
            case "Windows经典风格":
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
                break;
            case "Motif风格":
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
                break;
        }
        SwingUtilities.updateComponentTreeUI(f.getContentPane());
        SwingUtilities.updateComponentTreeUI(jMenuBar);
        SwingUtilities.updateComponentTreeUI(jPopupMenu);
    }
}